我正在尝试制作基本的点击计数器应用程序,它会计算点击次数,但我希望有一个 EditText 来设置所需的数字并点击,它会加倍向上并显示在 TextView 。
我尝试了accCount + tCount ++;
但它不起作用,
我尝试了另一个名为moon的用户提供的解决方案,它非常有用但是它本身就添加了自己的值。 我做错了什么?
代码:
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.patta.drawerrrrr.MainActivity"
tools:showIn="@layout/app_bar_main">
<TextView
android:id="@+id/touchCountText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/editText"
android:layout_alignParentStart="true"
android:background="@android:color/black"
android:gravity="center_horizontal"
android:onClick="touchFunc"
android:text="@string/_0"
android:textColor="#ffffff"
android:textSize="150sp" />
<EditText
android:id="@+id/editText"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_above="@+id/textView"
android:cursorVisible="false"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="number" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="59dp"
android:layout_above="@+id/adView"
android:layout_alignParentStart="true"
android:background="#ff1e00"
android:gravity="center"
android:onClick="reset"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:text="RESET"
android:textColor="#ffffff"
android:textStyle="bold"
app:layout_constraintRight_toRightOf="parent"
tools:layout_editor_absoluteY="400dp" />
JAVA:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Vibrator vibrator;
EditText editText;
TextView touchCountText;
int tCount = 0;
int accCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
touchCountText = findViewById(R.id.touchCountText);
editText = findViewById(R.id.editText);
int accCount = Integer.parseInt(editText.getText().toString());
}
public void touchFunc (View v){
tCount++;
touchCountText.setText(""+tCount);
vibrator.vibrate(40);
}
public void reset (View v){
tCount = 0;
touchCountText.setText(""+tCount);
vibrator.vibrate(100);
}
}
答案 0 :(得分:1)
试试这段代码,
Vibrator vibrator;
EditText editText;
TextView touchCountText;
int tCount = 0;
static double accCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
touchCountText =(TextView) findViewById(R.id.touchCountText);
editText = (EditText)findViewById(R.id.editText);
DrawerLayout drawer =(DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle
(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
public void touchFunc (View v){
if(!editText.getText().toString().isEmpty() && tCount==0)
accCount = Double.parseDouble(editText.getText().toString());
else
Toast.makeText(getApplicationContext(),"editText is empty",Toast.LENGTH_SHORT).show();
tCount++;
Log.d("value",String.valueOf(accCount));
touchCountText.setText(String.valueOf((double)tCount*accCount));
vibrator.vibrate(40);
}
public void reset (View v){
tCount = 0;
accCount=0;
touchCountText.setText(String.valueOf(tCount));
vibrator.vibrate(100);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
tCount--;
touchCountText.setText(String.valueOf((double)tCount*accCount));
vibrator.vibrate(40);
}
if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)){
tCount++;
touchCountText.setText(String.valueOf((double)tCount*accCount));
vibrator.vibrate(40);
}
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
onBackPressed();
}
return false;
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_camera) {
} else if (id == R.id.nav_share) {
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
答案 1 :(得分:0)
如果我没有误解你的问题
int valueFromEditText=0;
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
valueFromEditText=Integer.parseInt(editText.getText().toString().trim());
yourTextViewToDisplayDoubleValue.setText(valueFromEditText*2+"");
}
});
答案 2 :(得分:0)
你这样做是错误的
editText = findViewById(R.id.editText);
int accCount = Integer.parseInt(editText.getText().toString());
<强>原因强>:
当EditText刚刚初始化时,你不能简单地从EditText获取文本并将空文本解析成Integer,它可能会抛出异常。
所以你应该首先检查或者只是初始化EditText,默认值为 Zero 或者是第一次,然后单击某个按钮或者做类似的事情。
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
//your other code ...............
button.setOnClickListener(this);
//default value
editText.setText(String.valueOf(count));
}
@Override
public void onClick(View view){
//button click
count++
//double the value and show in editText
editText.setText(String.valueOf(count*2))
}