gpt4 book ai didi

android - 为什么不是我的 editText.requestFocus();在职的?

转载 作者:行者123 更新时间:2023-11-30 01:29:42 25 4
gpt4 key购买 nike

当我的应用程序启动时,默认情况下会加载软键盘。我这样做

final EditText editText = (EditText) findViewById(R.id.editText);
editText.requestFocus();

在我的 onCreate 方法中。在我的 XML 中,我的 editText 有 android:inputType="phone" ,这就是键盘出现的原因。

当用户滚动我的 ListView 时,键盘消失了,我也这样做了,也在 onCreate :
if (scrollState !=0){
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(listview.getWindowToken(), 0);
}

现在我在我的应用程序中加入了一个图像按钮。当用户单击它时,我希望键盘重新启动,但它不起作用。只是想知道为什么不,因为这似乎是一个非常基本的指令:
public void softkeyboardButton(View v)
{

final EditText editText = (EditText) findViewById(R.id.editText);
editText.requestFocus();
Log.e("button", "Yep, it worked.");

}

这是我的整个代码:
package com.example.chris.sunil_gupta;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.provider.CallLog;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.NumberPicker;

public class MainActivity extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {

//create a ListView object called listview
final ListView listview;


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listview = (ListView) findViewById(R.id.ListView_2);

String[] countries = {"Ireland", "France","England","Ireland", "France","England","Ireland", "France","England","Ireland", "France","England"};

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, countries);

listview.setAdapter(adapter);

final EditText editText = (EditText) findViewById(R.id.editText);
editText.requestFocus();


listview.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {



Log.e("scrollState", "Yep, it worked.");


if (scrollState !=0){
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(listview.getWindowToken(), 0);
}


}
// this method looks for changes in the edittext box.
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

}
});



editText.addTextChangedListener(new TextWatcher() {


@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
// If there are changes typed in the edittext, then change the height of the edittext from 0,
// as described in the activity_main xml file
public void afterTextChanged(Editable s) {

float density=getResources().getDisplayMetrics().density;

if (editText.length() > 0) {


// not sure what density and layoutparams do. I think we're
// converting pixels to dp
editText.getLayoutParams().height =(int)(50*density);
// editText.requestLayout();
Log.e("scrollState", "Yep, it worked.");

}
// if there is nothing in edittext, make it invisible
else if (editText.length() == 0){

editText.getLayoutParams().height =(int)(0*density);

}
editText.requestLayout();

}
});





}

public void softkeyboardButton(View v)
{

final EditText editText = (EditText) findViewById(R.id.editText);
editText.requestFocus();
Log.e("button", "Yep, it worked.");

}

//This clears the edittext next time user starts the application, rather than
// having the same numbers there, which the user probably doesn't want anymore
protected void onResume() {
final EditText editText = (EditText) findViewById(R.id.editText);
super.onResume();
editText.setText("");


}

}

最佳答案

试试这个

用于显示键盘

 InputMethodManager imm =   (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE); 
imm.showSoftInput(ed, 0);
ed.requestFocus();

希望这可以帮助你

关于android - 为什么不是我的 editText.requestFocus();在职的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35937566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com